﻿using HelixToolkit.Wpf;
using OptoSky.Common;
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Media3D;

namespace WpfApp3
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public ObservableCollection<Visual3D> Visual3DList { get; set; }
        private Wafer3D _waf;
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
            Visual3DList = new ObservableCollection<Visual3D>();
            Visual3DList.Add(new DefaultLights());
            //Visual3DList.Add(new TextVisual3D()
            //{
            //    Text = "0",
            //    BorderBrush = Brushes.Black,
            //    BorderThickness = new Thickness(0),
            //    Height = 6,
            //    FontSize = 50,
            //    FontWeight = FontWeight.FromOpenTypeWeight(150),
            //    Position = new Point3D(0, 0, 0),
            //    TextDirection = new Vector3D(1, 0, 0),
            //    UpDirection = new Vector3D(0, 1, 0)
            //});

            //测试数据，波浪形
            Point3D[,] points = new Point3D[100, 100];

            for (int i = 0; i < 100; i++)
            {
                for (int j = 0; j < 100; j++)
                {
                    float x = i / 10f;
                    float y = j / 10f;
                    float z = (float)Math.Sin(x) + (float)Math.Cos(y); // 自定义高度值
                    points[i, j] = new Point3D(x, z, y);
                }
            }

            //测试数据，球形
            //for (int i = 0; i < 100; i++)
            //{
            //    for (int j = 0; j < 100; j++)
            //    {
            //        double theta = i * 2 * Math.PI / 100;  // 纬度角度范围 [0, 2π]
            //        double phi = j * 2 * Math.PI / 100;        // 经度角度范围  [0, 2π]

            //        double x = Math.Sin(phi) * Math.Cos(theta);
            //        double y = Math.Sin(phi) * Math.Sin(theta);
            //        double z = Math.Cos(phi);
            //        points[i, j] = new Point3D(x, y, z);
            //    }
            //}


         
            _waf = new Wafer3D() { Points = LoadTemplate() };
            //_waf = new Wafer3D() { Points = points };
            Visual3DList.Add(_waf);

            double minZ = 3.991861;
            double maxZ = 3.991957;
            double zRange = maxZ - minZ;

            hv3d.ZoomExtents(new Rect3D(0, 0, minZ - zRange * 0.1, 1, 1, zRange * 1.2));

            // 获取HelixViewport3D控件的默认相机
            var defaultCamera = hv3d.Camera as PerspectiveCamera;

            // 设置相机的视角和位置
            defaultCamera.Position = new Point3D(0, 0, 200); // 设置相机位置
            defaultCamera.LookDirection = new Vector3D(0, 0, -1); // 设置相机观察方向
            defaultCamera.FieldOfView = 60; // 设置相机视角

            // 调整缩放比例999
            hv3d.ZoomExtents(); // 自动调整缩放比例以适应所有点的显示

        }


        public Point3D[,] LoadTemplate()
        {
            try
            {
                //string[] lines = File.ReadAllLines(@"E:\开发资源包\SM230\230中3D升级\WpfApp3\bin\Debug\3D点.csv");
                string[] lines = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + @"3D点.csv");

                Point3D[,] point3Ds = new Point3D[100, 100];
                for (int i = 0; i < 100; i++)
                {
                    for (int j = 0; j < 100; j++)
                    {
                        string[] values = lines[i * 100 + j].Split(',');

                        if (double.TryParse(values[0], out double x) &&
                            double.TryParse(values[1], out double y) &&
                            double.TryParse(values[2], out double z))
                        {
                            point3Ds[i, j] = new Point3D(x, y, z);
                        }
                    }
                }

                //double minZ = 3.991861;
                //double maxZ = 3.991957;
                //double avgZ = 3.9919;


                return point3Ds;
            }
            catch (Exception)
            {
                return null;
            }

        }

    }
}
